// imgWriter.cpp: implementation of the imgWriter class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "imgWriter.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

imgWriter::imgWriter()
{

}
imgWriter::imgWriter(string const & strPath):m_strFilePath(strPath)
{

}

imgWriter::~imgWriter()
{

}
typedef short int               SWORD;
typedef unsigned long int       UDWORD;
typedef unsigned short int      UWORD;


int imgWriter::SaveToBMP(UCHAR const * pData, string const & strPath)
{
	if (0 == pData || strPath.size()<1)
	{
#ifdef _DEBUG
		afxDump << ("SaveToBMP->inputs are invalid");
#endif
		return 0;
	}
	//save data;
	// port .c files
	int nWidth = m_nWidth;
	int nHigh = m_nHeight;
	UCHAR const * src  = pData;
	{
		SWORD x,y;
//		RGB *pixel;
		stBM_header BH;
		FILE *fp_bitmap;
		DWORD im_loc_bytes;
		BYTE nr_fillingbytes, i;
		BYTE zero_byte=0;
		
		fp_bitmap=fopen((strPath.c_str()),"wb");
		if (fp_bitmap==NULL) return 0;
		
		if (nWidth%4!=0) nr_fillingbytes=4-((nWidth*3L)%4);
		else nr_fillingbytes=0;
		
		BH.BMP_id = 'M'*256+'B';     fwrite(&BH.BMP_id,2,1,fp_bitmap);
/* Replaced by bob on 2010-04-13 for 
		BH.size=54+nHigh*(nWidth*3+nr_fillingbytes);fwrite(&BH.size,4,1,fp_bitmap);
*/// The new is:
		BH.size=54+nHigh*(nWidth+nr_fillingbytes);fwrite(&BH.size,4,1,fp_bitmap);		
// Replace end.
		BH.zero_res = 0;             fwrite(&BH.zero_res,4,1,fp_bitmap);
		BH.offbits = 54;             fwrite(&BH.offbits,4,1,fp_bitmap);
		BH.biSize = 0x28;            fwrite(&BH.biSize,4,1,fp_bitmap);
		BH.Width = nWidth;         fwrite(&BH.Width,4,1,fp_bitmap);
		BH.Height = nHigh;        fwrite(&BH.Height,4,1,fp_bitmap);
		BH.biPlanes = 1;             fwrite(&BH.biPlanes,2,1,fp_bitmap);
		BH.biBitCount = 8;          fwrite(&BH.biBitCount,2,1,fp_bitmap); //bits of pix
		BH.biCompression = 0;        fwrite(&BH.biCompression,4,1,fp_bitmap);
		BH.biSizeImage = 0;          fwrite(&BH.biSizeImage,4,1,fp_bitmap);
		BH.biXPelsPerMeter = 0xB40;  fwrite(&BH.biXPelsPerMeter,4,1,fp_bitmap);
		BH.biYPelsPerMeter = 0xB40;  fwrite(&BH.biYPelsPerMeter,4,1,fp_bitmap);
		BH.biClrUsed = 0;            fwrite(&BH.biClrUsed,4,1,fp_bitmap);
		BH.biClrImportant = 0;       fwrite(&BH.biClrImportant,4,1,fp_bitmap);
		
		im_loc_bytes=(DWORD)src+((DWORD)nHigh-1)*nWidth*4;

		//UCHAR * pSrc = const_cast<UCHAR*>(pData+nWidth*nHigh-nWidth);
		UCHAR * pSrc = const_cast<UCHAR*>(pData);
		for (y=0;y<nHigh;y++)
		{
			for (x=0;x<nWidth;x++)
			{
/* Replaced by bob on 2010-04-13 for 
				pixel=(RGB *)im_loc_bytes;
				fwrite(pixel, 3, 1, fp_bitmap);
*/// The new is:
				fwrite(pSrc+x, 1, 1, fp_bitmap);
// Replace end.
				im_loc_bytes+=4;
			}
//			for (i=0;i<nr_fillingbytes;i++)
				fwrite(&zero_byte,1,nr_fillingbytes,fp_bitmap);
			pSrc+=nWidth;//*4;
		}
		printf("Done.\n");
		fclose(fp_bitmap);
		return 1;
	}
}


int imgWriter::SaveToBMP2(UCHAR const * pData, string const & strPath)
{
	int i;
	BITMAPFILEHEADER fileHeader = {0};
	BITMAPINFOHEADER InfoHeader = {0};
	RGBQUAD ColorTable[256] = {0};
	int    nColroTable = 256;
	for (i=0;i<256;i++)
	{
		ColorTable[i].rgbRed   = (  0+i)%256;
/* Replaced by bob on 2010-04-14 for 
		ColorTable[i].rgbGreen = (100+i)%256;
		ColorTable[i].rgbBlue  = (200+i)%256;
*/// The new is:
		ColorTable[i].rgbGreen = (i)%256;
		ColorTable[i].rgbBlue  = (i)%256;

// Replace end.
	}

	fileHeader.bfType		= ((WORD) ('M' << 8) | 'B');	// is always "BM"
	fileHeader.bfSize		= m_nHeight*m_nWidth
								+ sizeof(InfoHeader)
								+ sizeof(fileHeader) 
								+ nColroTable * sizeof(RGBQUAD);
	fileHeader.bfReserved1 	= 0;
	fileHeader.bfReserved2 	= 0;
	fileHeader.bfOffBits	= sizeof(fileHeader) 
								+ sizeof(InfoHeader)
								+ nColroTable * sizeof(RGBQUAD);
	
	InfoHeader.biSize          = sizeof(InfoHeader);
	InfoHeader.biWidth         = m_nWidth;
	InfoHeader.biHeight        = m_nHeight;
	InfoHeader.biPlanes        = 1;
	InfoHeader.biBitCount      = 8;
	InfoHeader.biSizeImage     = m_nHeight * m_nWidth;
	InfoHeader.biXPelsPerMeter = 96;
	InfoHeader.biYPelsPerMeter = 96;

	FILE *fp_bitmap;
	fp_bitmap=fopen((strPath.c_str()),"wb");
	fwrite( &fileHeader, sizeof(fileHeader) , 1, fp_bitmap);
	fwrite( &InfoHeader, sizeof(InfoHeader) , 1, fp_bitmap);
	fwrite( ColorTable, nColroTable * sizeof(RGBQUAD),1, fp_bitmap);

	for (i=0; i<m_nHeight;i++)
	{
		fwrite(pData + (m_nHeight-i-1) * m_nWidth, m_nWidth, 1, fp_bitmap);
	}
	fclose(fp_bitmap);
	return 1;
}

	

void imgWriter::SetFilePath(string const & strPath)
{	
	m_strFilePath = strPath;	
}
void imgWriter::SetImageDimension(int nWidth, int nHight)
{
	m_nWidth = nWidth;
	m_nHeight = nHight;
}
int imgWriter::SaveToBMP(UCHAR const* pData, int nWidth, int nHight, string const& strPath )
{
	ASSERT(nWidth >0 && nHight >0);
	m_nWidth = nWidth;
	m_nHeight = nHight;
	return SaveToBMP2(pData, strPath);
}
